package com.czl.module_storehouse.activity.allocate.post;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.Button;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.base.BaseOperationActivity;
import com.czl.module_storehouse.adapter.AllocatePostOutAdapter;
import com.czl.module_storehouse.bean.AllocateBean;
import com.czl.module_storehouse.bean.AllocateOutBean;
import com.czl.module_storehouse.databinding.ActivityBaseOperationBinding;
import com.czl.module_storehouse.databinding.HeaderAllocatePostOutBinding;
import com.czl.module_storehouse.event.AllocateEvent;
import com.czl.module_storehouse.event.DetailsPostSuccessEvent;
import com.czl.module_storehouse.mvp.presenter.AllocatePresenter;
import com.czl.module_storehouse.mvp.view.AllocateView;
import com.jeremyliao.liveeventbus.LiveEventBus;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * @author xubin
 */
public class AllocatePostOutActivity extends BaseOperationActivity<ActivityBaseOperationBinding> implements AllocateView {

    private final AllocateOutBean mAllocateOutBean = new AllocateOutBean();
    private AllocatePostOutAdapter mAdapter;
    private HeaderAllocatePostOutBinding mHeaderBinding;
    @InjectPresenter
    AllocatePresenter mAllocatePresenter;

    @Override
    protected ActivityBaseOperationBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityBaseOperationBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("调拨出库");
        binding.btnSure.setText("调拨出库");
        binding.recyclerView.setAdapter(getAdapter());
    }

    @SuppressLint("SetTextI18n")
    private void initHeaderView(AllocateBean allocateBean) {
        mHeaderBinding.clHeaderInfo.ivHeader.setImageResource(R.mipmap.icon_lingyong_head);
        mHeaderBinding.clHeaderInfo.tvHeaderTitle.setText(allocateBean.getMoveName());
        mHeaderBinding.clHeaderInfo.tvHeaderCode.setText("调拨单：" + (allocateBean.getMoveCode() == null ? "" : allocateBean.getMoveCode()));
        mHeaderBinding.clHeaderInfo.tvHeaderDate.setText(allocateBean.getGmtCreate());
        mHeaderBinding.tvHeaderCompany.setText("申请公司：" + (allocateBean.getApplyCompanyName() == null ? "" : allocateBean.getApplyCompanyName()));
        mHeaderBinding.tvHeaderDepartment.setText("申请部门：" + (allocateBean.getApplyPersonDepartment() == null ? "" : allocateBean.getApplyPersonDepartment()));
        mHeaderBinding.tvStoreOut.setText("调出仓库：" + (allocateBean.getMoveStorehouseName() == null ? "" : allocateBean.getMoveStorehouseName()));
        mHeaderBinding.tvStoreIn.setText("调入仓库：" + (allocateBean.getInStorehouseName() == null ? "" : allocateBean.getInStorehouseName()));
        mHeaderBinding.tvReason.setText("调拨原因：" + (allocateBean.getApplyComment() == null ? "" : allocateBean.getApplyComment()));
        mHeaderBinding.tvOutCompany.setText("调出公司：" + (allocateBean.getMoveCompanyName() == null ? "" : allocateBean.getMoveCompanyName()));
    }

    @Override
    protected void submit() {
        String remark = mFooterBinding.etRemark.getText().toString();
        mAllocateOutBean.setMoveComment(remark);
        mAllocateOutBean.setSortList(mAdapter.getData());
        if (mAllocatePresenter != null) {
            mAllocatePresenter.editMoveInfo(mAllocateOutBean);
        }
    }

    @Override
    protected String getOperationTitle() {
        return "调拨";
    }

    @Override
    protected BaseQuickAdapter<SortBean, ?> getAdapter() {
        if (mAdapter == null) {
            mAdapter = new AllocatePostOutAdapter(R.layout.item_allocate_post_out, new ArrayList<>());
            mHeaderBinding = HeaderAllocatePostOutBinding.inflate(getLayoutInflater());
            mAdapter.addHeaderView(mHeaderBinding.getRoot());
            addFooterView(mAdapter);
            mAdapter.setOnItemChildClickListener((adapter, view, position)
                    -> startActivity(position - adapter.getHeaderLayoutCount()));
        }
        return mAdapter;
    }

    @Override
    protected Button getButton() {
        return binding.btnSure;
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(AllocateEvent event) {
        if (event != null && event.getAllocateBean() != null) {
            setupData(event.getAllocateBean());
        }
    }

    private void setupData(AllocateBean allocateBean) {
        mAllocateOutBean.setMoveId(allocateBean.getMoveId());
        mAllocateOutBean.setInCompany(allocateBean.isInCompany());
        initHeaderView(allocateBean);
        mAdapter.setList(allocateBean.getSortList());
    }

    @Override
    public <T> void showHttpResponse(HttpResponse<T> httpResponse) {
        super.showHttpResponse(httpResponse);
        if (AllocatePresenter.TAG_ALLOCATE_EDIT_MOVE_INFO.equals(httpResponse.getRequestTag())) {
            showToast("调拨成功");
            LiveEventBus.get(DetailsPostSuccessEvent.class).post(new DetailsPostSuccessEvent());
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHeaderBinding != null) {
            mHeaderBinding.unbind();
        }
    }
}
